package com.colorful.insuranceproject.claims;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.UploadDetailsBean;
import com.colorful.insuranceproject.commonActivity.WebViewActivity;
import com.colorful.insuranceproject.consts.URLConst;
import com.colorful.insuranceproject.dialog.ChoiceDateDialog;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.loginandresgiter.bean.SimpleUser;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/18.
 * <p>
 * 理赔设置  步骤一
 */
public class ClaimsStepOneActivity extends TitleActivity {

    private static final int REQUEST_CODE = 10000;

    public static final String EXTRA_UPLOAD_BEAN = "extra_upload_bean";

    public static final String EXTRA_STATUS = "EXTRA_STATUS";

    /**
     * 日期
     */
    @Bind(R.id.tv_date)
    TextView tvDate;

    /**
     * 理赔人员
     */
    @Bind(R.id.tv_person)
    TextView tvPerson;

    /**
     * 理赔单号
     */
    @Bind(R.id.tv_order_num)
    TextView tvOrderNum;

    UploadDetailsBean ub;

    private int status;

    @Override
    protected int getContentView() {
        return R.layout.activity_claims_step_one;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.step_one));
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        ub = bundle.getParcelable(EXTRA_UPLOAD_BEAN);
        status = bundle.getInt(EXTRA_STATUS);
    }

    @Override
    protected void onViewInitialized() {
        SimpleUser users = (SimpleUser) UserManager.getInstance().getCurrentUser();
        tvPerson.setText(users.getUserName());
        if (ub != null) {
            tvDate.setText(ub.getPaperTime());
            tvPerson.setText(ub.getUserName());
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @OnClick({R.id.tv_claims_encounter_question, R.id.tv_set, R.id.rl_date})
    void OnClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_claims_encounter_question:
                String url = URLConst.CLAIM_SETTLEMENT_AGREEMENT;
                launchActivity(WebViewActivity.class, WebViewActivity.EXTRA_LOAD_URL, url);
                break;
            case R.id.tv_set:
                if (tvDate.getText().equals(getString(R.string.please_select))) {
                    showToast(getString(R.string.please_choose_date));
                } else {
                    if (ub != null) {
                        ub.setUserName(tvPerson.getText().toString());
                        ub.setPaperTime(tvDate.getText().toString());
                        launchActivityForResult(ClaimsStepTwoActivity.class, REQUEST_CODE, ClaimsStepTwoActivity.EXTRA_UPLOAD_BEAN, ub,
                                ClaimsStepTwoActivity.EXTRA_STATUS, status);
                    } else {
                        launchActivityForResult(ClaimsStepTwoActivity.class, REQUEST_CODE, ClaimsStepTwoActivity.EXTRA_DATE, tvDate.getText().toString()
                                , ClaimsStepTwoActivity.EXTRA_NAME, tvPerson.getText().toString());
                    }
                }
                break;
            case R.id.rl_date:
                ChoiceDateDialog cdd = new ChoiceDateDialog();
                Bundle bundless = new Bundle();
                Calendar calendar = Calendar.getInstance();
                bundless.putInt(ChoiceDateDialog.EXTRA_MAX_YEAR, calendar.get(Calendar.YEAR));
                bundless.putInt(ChoiceDateDialog.EXTRA_MAX_MONTH, calendar.get(Calendar.MONTH));
                bundless.putInt(ChoiceDateDialog.EXTRA_MAX_DAY, calendar.get(Calendar.DAY_OF_MONTH));
                cdd.setArguments(bundless);
                cdd.setOnChoiceChangedListener(new ChoiceDateDialog.OnChoiceStateChangedListener() {
                    @Override
                    public void onChoiceCancel() {

                    }

                    @Override
                    public void onDateChoiced(int year, int month, int day, long millisecond) {
                        try {
                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(R.string.you_inputed_date_is_invalidate);
                        }
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
                        tvDate.setText(simpleDateFormat.format(millisecond));
                    }
                });
                cdd.show(getSupportFragmentManager(), cdd.getClass().getSimpleName());
                break;
        }
    }
}
